package com.czl.module_service.adapter.inventory

import android.os.Bundle
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.czl.base.binding.command.BindingCommand
import com.czl.base.binding.command.BindingConsumer
import com.czl.base.config.AppConstants
import com.czl.base.data.bean.tengyun.InventoryAssetBean
import com.czl.module_service.R
import com.czl.module_service.databinding.ItemInventoryHomeAssetBinding
import com.czl.module_service.fragment.inventory.InventoryHomeFragment

/**
 * @author by xubin
 * PS: Not easy to write code, please indicate.
 */
class InventoryHomeAdapter(private val fragment: InventoryHomeFragment) :
    BaseQuickAdapter<InventoryAssetBean, BaseDataBindingHolder<ItemInventoryHomeAssetBinding>>(
        R.layout.item_inventory_home_asset
    ) {
    override fun convert(
        holder: BaseDataBindingHolder<ItemInventoryHomeAssetBinding>,
        item: InventoryAssetBean
    ) {
        when (item.status) {
            0 -> {
                holder.getView<TextView>(R.id.tv_status).setTextColor(ContextCompat.getColor(context, R.color.color_007AFF))
                holder.getView<ImageView>(R.id.iv_asset).setImageResource(R.mipmap.icn_pd_weikaishi)
            }
            1 -> {
                holder.getView<TextView>(R.id.tv_status).setTextColor(ContextCompat.getColor(context, R.color.gray))
                holder.getView<ImageView>(R.id.iv_asset).setImageResource(R.mipmap.icn_pd_wancheng)
            }
            2 -> {
                holder.getView<TextView>(R.id.tv_status).setTextColor(ContextCompat.getColor(context, R.color.color_25A468))
                holder.getView<ImageView>(R.id.iv_asset).setImageResource(R.mipmap.icn_pd_pandianzhong)
            }
        }
        holder.dataBinding?.run {
            data = item
            adapter = this@InventoryHomeAdapter
            executePendingBindings()
        }
    }

    val onItemClick: BindingCommand<Any> = BindingCommand(BindingConsumer {
        if (it is InventoryAssetBean) {
            val bundle = Bundle()
            bundle.putString("execId", it.execId)
            it.taskId?.let { it1 -> bundle.putLong("taskId", it1) }
            bundle.putInt("status", it.status!!)
            bundle.putString("taskName", it.taskName)
            if (it.locationDesc == null) {
                it.locationDesc = ""
            }
            bundle.putString("locationDesc", it.locationDesc)
            fragment.startContainerActivity(
                AppConstants.Router.Service.F_SERVICE_ASSET_INVENTORY,
                bundle
            )
        }
    })
}